/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia;

import DAO.AccessDatabase;
import Objetos.Usuario;
import Objetos.Veiculo;
import Objetos.Veiculo_OS;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 *
 * @author celiobj
 */
public class RepositorioVeiculo implements RepositorioVeiculoInterface {

    @Override
    public void adicionar(Veiculo ve,Usuario usu) {

        try {

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
            st.execute("INSERT INTO `veiculos` (`marcaveiculo`,`idcliente`,`placa`,`cor`, `modelo`, `ano`) VALUES ('" + ve.getMarcaVeiculo() + "', '" + ve.getIdCliente()+ "', '" + ve.getPlacaVeiculo() + "', '" + ve.getCorCarro() + "', '" + ve.getModeloCarro() + "', '" + ve.getAnoCarro() + "');");

            JOptionPane.showMessageDialog(null, "Veículo cadastrado com sucesso");
        } catch (SQLException ex) {
            Logger.getLogger(RepositorioUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public Veiculo procurarPelaPlaca(String placa,Usuario usu) {

        Veiculo retorno;

        try {
            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();

            ResultSet rs = st.executeQuery("SELECT * FROM veiculos where placa = '" + placa + "'");
            if (rs.next()) {

                retorno = new Veiculo();
                retorno.setCodVeiculo(rs.getString("idveiculo"));
                retorno.setPlacaVeiculo(rs.getString("placa"));
                retorno.setModeloCarro(rs.getString("modelo"));
                retorno.setMarcaVeiculo(rs.getString("marcaveiculo"));
                retorno.setIdCliente(rs.getString("idcliente"));
                retorno.setAnoCarro(rs.getString("ano"));
                retorno.setCorCarro(rs.getString("cor"));

                return retorno;
            } else {
                return null;
            }
        } catch (SQLException ex) {

            Logger.getLogger(RepositorioVeiculo.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    @Override
    public JTable consultaTodos(Usuario usu) {

        try {
            Vector cabecalho = new Vector();
            Vector linhas = new Vector();
            AccessDatabase a = new AccessDatabase();
            JTable tabela;
            try (Connection con = a.conectar()) {
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("SELECT v.idveiculo, v.placa,c.nomecliente,v.modelo,v.marcaveiculo, v.cor, v.ano FROM veiculos v, clientes c WHERE c.idcliente=v.idcliente ORDER BY idveiculo");
                rs.next();
                ResultSetMetaData rsmd = rs.getMetaData();
                cabecalho.addElement("Cód");
                cabecalho.addElement("Placa");
                cabecalho.addElement("Cliente");
                cabecalho.addElement("Modelo");
                cabecalho.addElement("Marca");
                cabecalho.addElement("Cor");
                cabecalho.addElement("Ano");

                do {
                    linhas.addElement(proximaLinha(rs, rsmd));
                } while (rs.next());
                tabela = new JTable(linhas, cabecalho);
            }
            tabela.getColumnModel().getColumn(0).setPreferredWidth(80);
            tabela.getColumnModel().getColumn(1).setPreferredWidth(120);
            tabela.getColumnModel().getColumn(2).setPreferredWidth(200);
            tabela.getColumnModel().getColumn(3).setPreferredWidth(200);
            tabela.getColumnModel().getColumn(4).setPreferredWidth(200);
            tabela.getColumnModel().getColumn(5).setPreferredWidth(100);
            tabela.getColumnModel().getColumn(6).setPreferredWidth(100);

            return tabela;

        } catch (SQLException t) {
            System.out.println(t.getMessage());
            return null;

        }

    }

    private Vector proximaLinha(ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
        Vector LinhaAtual = new Vector();

        try {
            for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
                switch (rsmd.getColumnType(i)) {
                    case Types.VARCHAR:
                        LinhaAtual.addElement(rs.getString(i));
                        break;
                    case Types.TIMESTAMP:
                        LinhaAtual.addElement(rs.getDate(i).toLocaleString().substring(0, 10));
                        break;
                    case Types.INTEGER:
                        LinhaAtual.addElement(rs.getInt(i));
                        break;
                    case Types.DECIMAL:
                        LinhaAtual.addElement(Funcoes.funcoes.paraFormatoDinheiro(rs.getDouble(i)));
                        break;
                    case Types.DOUBLE:
                        LinhaAtual.addElement(Funcoes.funcoes.paraFormatoDinheiroRelatorio(rs.getDouble(i)).substring(3));
                        break;

                }
            }
        } catch (SQLException e) {
        }
        return LinhaAtual;

    }

    @Override
    public void inserirVeiculoOS(Veiculo_OS veos,Usuario usu) {

        try {

            AccessDatabase a = new AccessDatabase();
            Connection con = a.conectar();
            Statement st = con.createStatement();
            st.execute("INSERT INTO `veiculo_os` (`idVeiculo`, `idOs`,`valor`) VALUES ('" + veos.getIdVeiculo() + "', '" + veos.getIdOS() + "', '" + veos.getValor() + "');");

        } catch (SQLException ex) {
            Logger.getLogger(RepositorioUsuario.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}
